<template>
  <el-dialog :title="$t('letterReviewOrder.distribution')" :visible.sync="showState" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item :label="$t('letterReviewOrder.commissioner')" prop="staffId">
        <el-cascader
          v-model="formValidate.staffId"
          clearable
          :options="allStaff"
          :props="{ expandTrigger: 'hover' }"
          :placeholder="$t('letterReviewOrder.prompt.commissioner')"
          style="width: 100%"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.operate.sure') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { allocationCreditOrder } from '@/api/risk'
export default {
  name: 'AllocationDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    creditOrderIds: {
      type: Array,
      default: function() {
        return []
      }
    },
    allStaff: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        staffId: null
      },
      rules: {
        staffId: [{ required: true, message: this.$t('letterReviewOrder.commissioner') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.$refs['formValidate'].clearValidate()
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.submitLoading = true
          const data = {
            creditOrderIds: this.creditOrderIds,
            staffId: this.formValidate.staffId[2]
          }
          allocationCreditOrder(data)
            .then(res => {
              this.submitLoading = false
              if (res.success) {
                this.$message.success(this.$t('letterReviewOrder.distributionSuccess'))
                this.$emit('edit-success')
                this.handleClose()
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
              this.submitLoading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
